package com.kaibes.platform.article.admin;

import java.math.BigInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.kaibes.web.admin.AdminController;
import com.kaibes.platform.article.db.service.ArticleCommentService;
import com.kaibes.platform.article.db.service.ArticleService;
import com.kaibes.web.api.ApiLink;

@AdminController
@RequestMapping("/admin/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleCommentService articleCommentService;
    
    @GetMapping("/edit")
    @ApiLink("文章创建页面")
    public String edit(Model model) {
        return "article-edit";
    }
    
    @GetMapping("/edit/{id:\\d+}")
    @ApiLink("文章编辑页面")
    public String edit(Model model, @PathVariable BigInteger id) {
        model.addAttribute("article", articleService.getById(id));
        return "article-edit";
    }
    
    @GetMapping("/view")
    @ApiLink("文章浏览页面")
    public String view(Model model, 
            @RequestParam(defaultValue = "25") int size, 
            @RequestParam(defaultValue = "1") int page, 
            @RequestParam(defaultValue = "") String search,
            @RequestParam(required = false) Short cId,
            @RequestParam(required = false) String tag) {
        if (cId != null) {
            model.addAttribute("articlePage", articleService.pageAscByStatusAndCategoryId(page, size, null, cId));
        } else if (tag != null) {
            model.addAttribute("articlePage", articleService.pageAscByStatusAndTag(page, size, null, tag));
        } else {
            search = search.trim();
            if (!search.isEmpty()) {
                model.addAttribute("articlePage", articleService.pageAscByStatusAndSearch(page, size, null, search));
            } else {
                model.addAttribute("articlePage", articleService.pageAscByStatus(page, size, null));
            }
        }
        return "article-view";
    }
    
    @GetMapping("/category")
    @ApiLink("文章分类页面")
    public String category(Model model) {
        return "article-category";
    }
    
    @GetMapping("/tag")
    @ApiLink("文章标签页面")
    public String tag(Model model) {
        return "article-tag";
    }
    
    @GetMapping("/comment")
    @ApiLink("评论管理页面")
    public String comment(Model model, 
            @RequestParam(defaultValue = "25") int size,
            @RequestParam(defaultValue = "1") int page, 
            @RequestParam(defaultValue = "") String search) {
        search = search.trim();
        if (!search.isEmpty()) {
            model.addAttribute("commentPage", articleCommentService.pageDescBySearch(page, size, search));
        } else {
            model.addAttribute("commentPage", articleCommentService.page(page, size));
        }
        return "article-comment";
    }
}
